package client.protobuf;

import bean.GatewayProtobufProtocol;
import codec.GatewayProtocolCodec;
import com.google.protobuf.ByteString;
import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.protobuf.ProtobufDecoder;
import io.netty.handler.codec.protobuf.ProtobufEncoder;
import io.netty.handler.codec.protobuf.ProtobufVarint32FrameDecoder;
import io.netty.handler.codec.protobuf.ProtobufVarint32LengthFieldPrepender;

/**
 * 编写一个 main() 方法来启动客户端。
 */
public class ProtobufClient extends Thread {
    public static void main(String[] args) throws Exception {
//        String host = "123.57.161.210";
//        String host = "118.242.40.252";
        String host = "127.0.0.1";

        for (int i = 0; i < 1; i++) {
            new ProtobufClient(host, 18099, (byte) i).start();
        }

    }

    private final String host;
    private final int port;
    private final byte id;


    public ProtobufClient(String host, int port, byte id) {
        this.host = host;
        this.port = port;
        this.id = id;
    }

    public void run() {
        EventLoopGroup group = new NioEventLoopGroup();
        try {
            Bootstrap bootstrap = new Bootstrap()
                    .group(group)
                    .channel(NioSocketChannel.class)
                    .handler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel socketChannel) throws Exception {
                            ChannelPipeline pipeline = socketChannel.pipeline();
                            pipeline.addLast(new ProtobufVarint32FrameDecoder());
                            pipeline.addLast(new ProtobufDecoder(GatewayProtobufProtocol.ProtobufProtocol.getDefaultInstance()));
                            pipeline.addLast(new ProtobufVarint32LengthFieldPrepender());
                            pipeline.addLast(new ProtobufEncoder());
                            pipeline.addLast(new ProtobufClientHandler());
                        }
                    });
            Channel channel = bootstrap.connect(host, port).sync().channel();
            //BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

            while (true) {
                Thread.sleep(1000);

//                for (int i=0;i<b.length;i++){
//                    System.out.print(byteTo16(b[i])+ " ");
//                }
//                System.out.println();
                for (int i = 0; i < 1; i++) {
                    GatewayProtobufProtocol.ProtobufProtocol.Builder protocol = GatewayProtobufProtocol.ProtobufProtocol.newBuilder();
                    protocol.setLenght(20);
                    protocol.addDatas(ByteString.copyFrom(new byte[]{(byte) 0x88, (byte) 0x87, (byte) 0xFA}));
                    ChannelFuture future = channel.writeAndFlush(protocol);
                    future.addListener(future1 -> System.out.println("==>" + future.isSuccess()));

//                    Thread.sleep(1000);
                }

            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            group.shutdownGracefully();
        }

    }

    public static String byteTo16(byte bt) {
        String[] strHex = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"};
        String resStr = "";
        int low = (bt & 15);
        int high = bt >> 4 & 15;
        resStr = strHex[high] + strHex[low];
        return resStr;
    }
}
